<?php
/*
 * PHPYell
 * Useful functions for PHP facilities
 * http://joaobbn.com.br/projects/php-yell
 *
 * Copyright 2013 João BBN (João Baptista Bueno Neto)
 * Released under the MIT license
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 * 
 */

// @todo error_callback
function get ($url, $success_callback = FALSE, $error_callback = FALSE) {
    $data_return = file_get_contents($url,FILE_TEXT);
    
    if (is_callable($success_callback))
        $success_callback($data_return);
    
    if (is_callable($error_callback))
        $error_callback($data_return);
}

function my_each ($array, $iterate_callback = FALSE) {
    if (!is_array($array))
        return FALSE;
    
    if (is_callable($iterate_callback) && is_array($array)) {
        do {
            $index = key($array);
            
            $iterate_callback($index, $array[$index]);
        } while ($next = next($array));
    }
}



function load ($to_load, &$app, $success_callback = FALSE, $error_callback = FALSE) {
    // loads one 'assets'
    if (!is_array($to_load)) {
        // _load_one($to_load)';
        $app -> database = "DATABASE";
        $app -> runtime -> log -> load[] = "$to_load: loaded";
    } elseif(is_array($to_load)) {
        
    }
    
    if (is_callable($success_callback))
        $success_callback($data_return);
}



/*
CONFIG file
    autoload
    routes
    ...
*/

function app ($app_name, $success_callback = FALSE, $error_callback = FALSE) {
    $app -> name = $app_name;
    $app -> config = array(
        'autoload' =>array('get_helper')
    ); // parse YAML // YAML, then cache to PHP?
    
    // if app -> config not empty, app exists
    if (!empty($app -> config) && is_callable($success_callback)) {
        $success_callback($app);
    } elseif (is_callable($error_callback))
        $error_callback($app);
}

function app_refresh ($app_name) {
     // recache the config file
}

function browser () {
    $b -> name = server()->HTTP_USER_AGENT;
    $b -> get = $_GET;
    $b -> post = $_POST;
    
    return $b;
}

function server () {
    return (object) $_SERVER;
}

function user ($id) {
    $u -> id = $id;
    $u -> name = 'John';
    return $u;
}

class Yell {
    public $value=NULL;
    
    function __construct () {
        
    }
    function fn () {
        $this->doOnce = 'asd';
        
        return $this;
    }
    
    public function each (Closure $callback) {
        if (is_callable($callback) && is_array($this->value)) {
            foreach ($this->value as $key => $value)
                $callback($key,$value);
        }
    }
    
    public function up () {
        $this->value = strtoupper($this->value);
        return $this;
    }
    
    public function out ($return_=FALSE) {
        if (TRUE === $return_)
            return $this->value;
        else
            echo $this->value;
    }
    
    public function __invoke ($param) {
        $this->value = $param;
        return $this;
    }
}

// extending the core
class Yell_fn {
    function __construct (&$parent) {
        $this->parent = &$parent;
    }
    
    #############################@@@@@@@@@@@@@@@ @todo trying a pre/post tratament
    function __call ($method,$param) {
        echo 'pre tratament';

        //return $this->$method($param);
    }
}

// get the app instance anywhere
function get_instance () {
    return $GLOBALS['_'];
}

## AAHH! "Yell if you are paying attention!"
$app =  APP_NAME;
include APP_NAME.'.php';
$_ = new $app();
